package com.example.administrator.yaoyiyaotest01;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    MyView myView ;
    private SensorManager sm;
    private Sensor sensor;
    private float lastX;
    private float lastY;
    private float lastZ;
    private int id0;
    private static final int SPEED_SHRESHOLD = 5000;
    // 两次检测的时间间隔
    private static final int UPTATE_INTERVAL_TIME = 50;
    private long lastUpdateTime;
    private SoundPool sp = null;
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myView = (MyView)findViewById(R.id.mView_text);

        sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener( this,sensor,SensorManager.SENSOR_DELAY_UI);

//加载声音-------------------------------------------------------
        int currenttapiVersion = Build.VERSION.SDK_INT;
        if (currenttapiVersion < 21){
            sp = new SoundPool(10, AudioManager.STREAM_SYSTEM,5);
        }else {
            SoundPool.Builder spb = new SoundPool.Builder();
            //类似于线程池子。可以同时播放10个声音。
            spb.setMaxStreams(10);

            sp = spb.build();


            try {
                AssetFileDescriptor afd1 = getAssets().openFd("sound/m70.wav");
                sp.load(afd1.getFileDescriptor(),afd1.getStartOffset(),afd1.getLength(),1);
                id0 =   sp.load(afd1,1);



            } catch (IOException e) {
                e.printStackTrace();
            }

        }




    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        System.out.println("-------------->value1"+event.values[0]);//绕z轴旋转
        System.out.println("-------------->value2"+event.values[1]);//绕x轴旋转
        System.out.println("-------------->value3"+event.values[2]);//绕y轴旋转

        long currentUpdateTime = System.currentTimeMillis();
        // 两次检测的时间间隔
        long timeInterval = currentUpdateTime - lastUpdateTime;
        // 判断是否达到了检测时间间隔
        if (timeInterval < UPTATE_INTERVAL_TIME) return;
        // 现在的时间变成last时间
        lastUpdateTime = currentUpdateTime;
        // 获得x,y,z坐标
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];
        // 获得x,y,z的变化值
        float deltaX = x - lastX;
        float deltaY = y - lastY;
        float deltaZ = z - lastZ;
        // 将现在的坐标变成last坐标
        lastX = x;
        lastY = y;
        lastZ = z;
        double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ
                * deltaZ)
                / timeInterval * 10000;
        myView.setText("摇一摇"+speed);
        // 达到速度阀值，发出提示
        if (speed >= SPEED_SHRESHOLD)
        {
            //加载声音


            sp.play(id0,1,1,0,0,1);

        }



    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        sm.unregisterListener(this);

    }

}
